package com.ikangtai.shecare.common.baseView.pickerDialog.recordPickerDialog;

import android.content.Context;
import android.widget.FrameLayout;
import android.widget.NumberPicker;
import com.ikangtai.shecare.R;

/**
 * Created by Administrator on 2016/1/23.
 */
public class TemperaturePicker extends FrameLayout {
    //    private final NumberPicker mDateSpinner;
    private final NumberPicker mHourSpinner;
    private final NumberPicker mMinuteSpinner;
    private int mDegree = 36;
    private int mMinute = 50;
    private OnTemperatureChangedListener mOnTemperatureChangedListener;

    public TemperaturePicker(Context context,String temper) {
        super(context);
        /**
         * 加载布局
         */
        inflate(context, R.layout.dialog_temperature_record, this);
        /**
         * 初始化控件
         */
        mDegree = Integer.valueOf(temper.substring(0,2));
        mMinute = Integer.valueOf(temper.substring(3));
        mHourSpinner = (NumberPicker) this.findViewById(R.id.temperature_degree);
        mHourSpinner.setMaxValue(42);
        mHourSpinner.setMinValue(35);
        mHourSpinner.setValue(mDegree);
        mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);
        mHourSpinner.clearFocus();
        mHourSpinner.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        mMinuteSpinner = (NumberPicker) this.findViewById(R.id.temperature_minute);
        mMinuteSpinner.setMaxValue(99);
        mMinuteSpinner.setMinValue(0);
        mMinuteSpinner.setValue(mMinute);
        mMinuteSpinner.setFormatter(new NumberPicker.Formatter() {
            @Override
            public String format(int value) {
                String Str = String.valueOf(value);
                if (value < 10) {
                    Str = "0" + Str;
                }
                return Str;
            }
        });
        mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
        mMinuteSpinner.clearFocus();
        mMinuteSpinner.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    }
    /**
     *
     * 控件监听器
     */
    private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            mDegree = mHourSpinner.getValue();
            onTemperatureChanged();
        }
    };

    private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            mMinute = mMinuteSpinner.getValue();
            onTemperatureChanged();
        }
    };


    /*
     *接口回调 参数是当前的View 年月日小时分钟
     */
    public interface OnTemperatureChangedListener {
        void onTemperatureChanged(TemperaturePicker view, int hour, int minute);
    }
    /*
     *对外的公开方法
     */
    public void setOnTemperatureChangedListener(OnTemperatureChangedListener callback) {
        mOnTemperatureChangedListener = callback;
    }

    private void onTemperatureChanged() {
        if (mOnTemperatureChangedListener != null) {
            mOnTemperatureChangedListener.onTemperatureChanged(this, mDegree, mMinute);
        }
    }
}
